package concurrent;

import java.util.concurrent.TimeUnit;

/**
 * @author 26029
 * @date 2025/4/24
 * @description
 */
public class LearnVolatile {
    private static volatile boolean stop = false;

    public static void main(String[] args) {
        Thread threadA = new Thread("Thread A") {
            @Override
            public void run() {
                while (!stop) {}
                System.out.println(Thread.currentThread().getName() + "停止了");
            }
        };
        threadA.start();

        try {
            TimeUnit.SECONDS.sleep(1);
            System.out.println("等待一秒");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("设置为true");
        LearnVolatile.stop = true;
    }
}
